<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="客户号" prop="ecifId">
        <el-input
          v-model="queryParams.ecifId"
          placeholder="请输入客户号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="工单号" prop="orderNo">
        <el-input
          v-model="queryParams.orderNo"
          placeholder="请输入工单号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="券码" prop="voucherNo">
        <el-input
          v-model="queryParams.voucherNo"
          placeholder="请输入券码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="车牌号" prop="license">
        <el-input
          v-model="queryParams.license"
          placeholder="请输入车牌号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="状态" prop="state">
        <el-select v-model="queryParams.state" placeholder="请选择订单状态" clearable size="small">
          <el-option
            v-for="dict in orderStateOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="回传状态" prop="state">
        <el-select v-model="queryParams.echoState" placeholder="请选择订单状态" clearable size="small">
          <el-option
            v-for="dict in echoStateOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="订单类型" prop="orderType">
        <el-select v-model="queryParams.orderType" placeholder="请选择订单类型 " clearable size="small">
          <el-option
            v-for="dict in orderTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间" prop="gmtTime">
        <el-date-picker
          v-model="dateRange"
          :unlink-panels="unlinkPanels"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="完成时间">
        <el-date-picker
          v-model="finishDateRange"
          :unlink-panels="unlinkPanels"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">

      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['dadi:preorder:export']"
        >导出</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="preorderList" @selection-change="handleSelectionChange">
      <el-table-column label="客户号" align="center" prop="ecifId" />
      <el-table-column label="工单号" align="center" prop="orderNo" />
      <el-table-column label="券码" align="center" prop="voucherNo" />
      <el-table-column label="状态" align="center" prop="state" :formatter="orderStateFormat"/>
      <el-table-column label="回传状态" align="center" prop="echoState" :formatter="echoStateFormat"/>
      <el-table-column label="车架号" align="center" prop="vin" />
      <el-table-column label="车牌号" align="center" prop="license" />
      <el-table-column label="订单类型" align="center" prop="orderType" :formatter="orderTypeFormat"/>
      <el-table-column label="实付金额" align="center" prop="actPrice" />
      <el-table-column label="发动机号" align="center" prop="engineId" />
      <el-table-column label="年限" align="center" prop="yearVehicle" />
      <el-table-column label="车辆注册日期" align="center" prop="vehicleRegisterTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.vehicleRegisterTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="座位数" align="center" prop="seats" />
      <el-table-column label="备注" align="center" prop="remark" />

      <el-table-column label="生成时间" align="center" prop="gmtTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.gmtTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="服务开始时间" align="center" prop="startTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="服务到期时间" align="center" prop="endTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作"  fixed="right" width="150" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.orderId"
            size="mini"
            type="text"
            icon="el-icon-s-unfold"
            @click="handleOrder(scope.row)"
            v-hasPermi="['order:order:detail']"
          >订单详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改大地保险预下单对话框 -->
    <el-dialog title="订单详情" :visible.sync="open" v-if="open" width="75%">
      <order-detail :orderId="orderId"></order-detail>
    </el-dialog>
  </div>
</template>

<script>
import { listPreorder, getPreorder, delPreorder, addPreorder, updatePreorder, exportPreorder } from "@/api/third/dadi";
import OrderDetail  from "../../order/order/OrderDetail";

export default {
  name: "Preorder",
  components: {OrderDetail},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      unlinkPanels:true,
      // 总条数
      total: 0,
      // 大地保险预下单表格数据
      preorderList: [],
      dateRange: [],
      finishDateRange: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        ecifId: undefined,
        startTime: undefined,
        endTime: undefined,
        orderNo: undefined,
        voucherNo: undefined,
        license: undefined,
        state: undefined,
        echoState: undefined,
        orderType: undefined,
        gmtStartTime: undefined,
        gmtEndTime: undefined,
        finishStartTime: undefined,
        finishEndTime: undefined,
      },
      orderStateOptions: [],
      echoStateOptions: [],
      orderTypeOptions: [],
      orderId: undefined,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        ecifId: [
          { required: true, message: "大地（客户号）不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("dadi_order_state").then(response => {
      this.orderStateOptions = response.data;
    });
    this.getDicts("dadi_echo_state").then(response => {
      this.echoStateOptions = response.data;
    });
    this.getDicts("order_type").then(response => {
      this.orderTypeOptions = response.data;
    });
  },
  methods: {
    handleOrder(row){
      this.open = true;
      this.orderId = row.orderId;
    },
    addGmtDateRange(params, dateRange) {
      var search = params;
      search.gmtStartTime = "";
      search.gmtEndTime = "";
      if (null != dateRange && '' != dateRange) {
        search.gmtStartTime = dateRange[0];
        search.gmtEndTime = dateRange[1];
      }
      return search;
    },
    addFinishDateRange(params, dateRange) {
      var search = params;
      search.finishStartTime = "";
      search.finishEndTime = "";
      if (null != dateRange && '' != dateRange) {
        search.finishStartTime = dateRange[0];
        search.finishEndTime = dateRange[1];
      }
      return search;
    },
    /** 查询大地保险预下单列表 */
    getList() {
      this.loading = true;
      this.addGmtDateRange(this.queryParams,this.dateRange);
      this.addFinishDateRange(this.queryParams,this.finishDateRange);
      listPreorder(this.queryParams).then(response => {
        this.preorderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        ecifId: undefined,
        orgCode: undefined,
        startTime: undefined,
        endTime: undefined,
        orderNo: undefined,
        voucherNo: undefined,
        serviceProductId: undefined,
        vin: undefined,
        license: undefined,
        engineId: undefined,
        yearVehicle: undefined,
        vehicleRegisterTime: undefined,
        seats: undefined,
        remark: undefined,
        version: undefined,
        state: undefined,
        gmtTime: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加大地保险预下单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getPreorder(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改大地保险预下单";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updatePreorder(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          } else {
            addPreorder(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除大地保险预下单编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delPreorder(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有大地保险预下单数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportPreorder(queryParams);
        }).then(response => {
          this.download(response);
        const blob = new Blob([response], { type: 'application/vnd.ms-excel' }) // res就是接口返回的文件流了
        const objectUrl = URL.createObjectURL(blob)
        window.location.href = objectUrl
        }).catch(function() {});
    },
    // 订单状态字典翻译
    orderStateFormat(row, column) {
      return this.selectDictLabel(this.orderStateOptions, row.state);
    },
    // 回传状态字典翻译
    echoStateFormat(row, column) {
      return this.selectDictLabel(this.echoStateOptions, row.echoState);
    },
    orderTypeFormat(row, column) {
      return this.selectDictLabel(this.orderTypeOptions, row.orderType);
    },
  }
};
</script>
